/*
 * upload_process.cpp
 *
 *  Created on: 22 May 2011
 *      Author: cccp
 */

#include "headers/upload_process.hpp"

#include <QDebug>
#include <QCoreApplication>
#include <QFileInfo>

//-----------------------------------------------------------------------------------------------------------------

UploadProcess::UploadProcess(QString _apiKey, QString _uploadFile, bool _lastUpload)
{
    if (!_apiKey.isEmpty())
    {
        apiKey = _apiKey;
    }

    lastUpload = false;

    if (_lastUpload)
    {
        lastUpload = _lastUpload;
    }

    if (!_uploadFile.isEmpty())
    {
        uploadFile = _uploadFile;
        serverId = -1;
    }
}

//-----------------------------------------------------------------------------------------------------------------

UploadProcess::~UploadProcess()
{
    delete ifileServerComms;
    ifileServerComms = NULL;
}

//-----------------------------------------------------------------------------------------------------------------

void UploadProcess::run()
{
    ifileServerComms = new IfileServerComms(apiKey);

    QFileInfo fileInfo (uploadFile);
    fileSize = fileInfo.size();

    connect(ifileServerComms, SIGNAL(serverIdFinished(IfileServerComms::UploadServerResponse)),
            this, SLOT(setServerID(IfileServerComms::UploadServerResponse)));

    connect(ifileServerComms, SIGNAL(uploadFinished(IfileServerComms::UploadFileResponse)),
            this, SLOT(uploadProcessFinished(IfileServerComms::UploadFileResponse)));

    connect(ifileServerComms, SIGNAL(uploadProgress(int)), this, SLOT(currentProgress(int)));

    connect(ifileServerComms, SIGNAL(httpReplyError(IfileServerComms::HttpError)),
            this, SLOT(uploadError(IfileServerComms::HttpError)));

    // get server id
    ifileServerComms->getUploadServerId();

    // 100mS * 600 = 60S
    int delay = 600;
    int waiting = 0;

    while (serverId == -1)
    {
        QThread::msleep(100);
        QCoreApplication::processEvents();

        if (waiting >= delay)
        {
            // Quit
            serverId = 0;
        }

        waiting++;
    }

    // start uploading
    if (serverId && !uploadFile.isEmpty())
    {
        if (!folderKey.isEmpty())
        {
            ifileServerComms->setFolderKey(folderKey);
        }

        if (!ifileServerComms->uploadFile(uploadFile, serverId))
        {
            finishedUploading = false;

            while (!finishedUploading)
            {
                QThread::msleep(100);
                QCoreApplication::processEvents();
            }

        }
    }

    if (lastUpload)
    {
        emit lastUploadFinished();
    }
    else
    {
        emit uploadFinished();
    }
}

//----------------------------------------------------------------------------------------

void UploadProcess::setServerID(IfileServerComms::UploadServerResponse uploadServerResponse)
{
    if (!uploadServerResponse.errorCode)
    {
        serverId = uploadServerResponse.serverId;
    }
    else
    {
        serverId = 0;
    }
}

//----------------------------------------------------------------------------------------

void UploadProcess::uploadProcessFinished(IfileServerComms::UploadFileResponse /*uploadFileResponse*/)
{
    finishedUploading = true;
}

//----------------------------------------------------------------------------------------

void UploadProcess::uploadError(IfileServerComms::HttpError /*httpError*/)
{
    finishedUploading = true;
}

//----------------------------------------------------------------------------------------

void UploadProcess::currentProgress(int uploadProgressLevel)
{
    emit uploadProgress(uploadProgressLevel, fileSize);
}

//----------------------------------------------------------------------------------------

void UploadProcess::setFolderKey(QString _folderKey)
{
    folderKey = _folderKey;
}


